package com.john.elasticsearch.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.john.utils.enums.PageEnums;
import com.john.utils.enums.ResultStatus;
import com.john.utils.result.AjaxResult;
import com.john.utils.token.TokenSing;

import java.util.List;

/**
 * searchAfter分页查询自定义返回
 */
public class SearchAfterResult<T> {
    /**SearchAfter分页查询交替值*/
    private String hands;
    // 单页显示数据条数
    private Integer pageSize = PageEnums.P20.getCode();
    //是否下一页
    private boolean pageNext = true;
    /**结果*/
    private List<T> rows;

    private Integer rowsSzie = 0;

    @JsonIgnore  //返回时排除掉这个字段
    private String handsDefault = "notData";

    public SearchAfterResult(){}

    @JsonIgnore  //返回时排除掉这个字段
    private AjaxResult ajaxResult;

    /**
     * 参数效验
     * @return true效验通过 false 效验不通过
     */
    public boolean validator(){
        this.pageSize = (pageSize == null || pageSize <= 0) ? PageEnums.P20.getCode() : pageSize;
        if(this.hands != null && !this.hands.equals("")){
            String handsDec = TokenSing.decrypt(getHands());
            if(handsDec == null || handsDec.equals("")){
                this.ajaxResult = new AjaxResult(ResultStatus.HANDS.getCode(),ResultStatus.HANDS.getMsg());
                return false;
            }
            String[] param = handsDec.split("-");
            if(param.length > 0 && param[0].equals(this.handsDefault)){
                this.ajaxResult = new AjaxResult(ResultStatus.SUCCESS.getCode(),ResultStatus.SUCCESS.getMsg(),this);
                return false;
            }
        }
        /***如果有结果集且pageSize小于20或者大于最大数据条数时*/
        if(this.pageSize < PageEnums.PMINPAGESIZE.getCode() || this.pageSize > PageEnums.PMAXPAGESIZE.getCode()){
            this.ajaxResult = new AjaxResult(ResultStatus.PAGESIZE.getCode(),ResultStatus.PAGESIZE.getMsg());
            return false;
        }
        return true;
    }

    public String getHands() {
        return hands;
    }

    public void setHands(String hands) {
        this.hands = hands;
    }

    public List<T> getRows() {
        return rows;
    }

    public void setRows(List<T> rows) {
        this.rows = rows;
        this.rowsSzie = rows.size();
    }

    public AjaxResult getAjaxResult() {
        return ajaxResult;
    }

    public void setAjaxResult(AjaxResult ajaxResult) {
        this.ajaxResult = ajaxResult;
    }

    public String getHandsDefault() {
        return handsDefault;
    }

    public void setHandsDefault(String handsDefault) {
        this.handsDefault = handsDefault;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public boolean isPageNext() {
        return pageNext;
    }

    public void setPageNext(boolean pageNext) {
        this.pageNext = pageNext;
    }

    public Integer getRowsSzie() {
        return rowsSzie;
    }

    public void setRowsSzie(Integer rowsSzie) {
        this.rowsSzie = rowsSzie;
    }
}
